// const messageUtil = require('../../utils/messageUtil.js')
import messageUtil from "../../utils/messageUtil"
import mobileApi from "../../api/mobileApi"
import commonData from "../../api/data"
import lbkOrderApi from "../../api/lbkOrderApi"
import logApi from "../../api/logApi"

Component({
  mixins: [],
  data: {
    counter:'',
    formData: {
      username: '',
      phone: '',
      code: '',
      isAgreement: false,
    },
    mobileList: [
      '177***  3分钟前  领取成功',
      '138***  3分钟前  领取成功',
      '181***  3分钟前  领取成功',
      '139***  3分钟前  领取成功',
      '177***  3分钟前  领取成功',
      '138***  3分钟前  领取成功',
      '181***  3分钟前  领取成功',
      '139***  3分钟前  领取成功',

    ],
    isShowAgreement: false,
    agreementType: 1,
    isShowAgreementConfirm: false,
    isShowSmsConfirm: false,
    timeData: {
      lastTime: 0,
      hour: 23,
      minute: 59,
      second: 59,
      milliseconds: 100
    },
    oder:"",
    showTimeIntervalId: 0,
    mobileList: [

      '177***  抢到了',
      '138***  抢到了',
      '181***  抢到了',
      '139***  抢到了',
      '177***  抢到了',
      '138***  抢到了',
      '181***  抢到了',
      '139***  抢到了',
    ]
  },
  props: {
    rootImagePath: '',
    onGoToPay:data=>{
      console.log(data)
    }
  },
  didMount() {
    // this.$page.keepAgr
    this.initShowTimeMethod()
  },
  didUpdate() {
    console.log(this.data.oder)
  },
  didUnmount() {
 
  },
  onInit(){


  },
  methods: {

    initShowTimeMethod() {
      let lastTime = new Date();
      // lastTime.setHours(23);
      // lastTime.setMinutes(59);
      // lastTime.setSeconds(59);
      lastTime.setMinutes(lastTime.getMinutes() + 5)

      let timeData = this.data.timeData
      timeData.lastTime = lastTime.getTime();
      this.setData({
        timeData: timeData
      })
      let intervalId = setInterval(() => {
        this.showTimeMethod();
      }, 10);

      this.setData({
        showTimeIntervalId: intervalId,
      })

    },
    showTimeMethod() {
      var currentDate = new Date(); // 获取当前时间
      var time_now = currentDate.getTime();
      var time_distance = this.data.timeData.lastTime - time_now; // 结束时间减去当前时间
      var int_day, int_hour, int_minute, int_second, int_milliseconds;

      let timeData = this.data.timeData

      if (time_distance >= 0) {
        // 天时分秒换算
        int_day = Math.floor(time_distance / 86400000)
        time_distance -= int_day * 86400000;
        int_hour = Math.floor(time_distance / 3600000)
        time_distance -= int_hour * 3600000;
        int_minute = Math.floor(time_distance / 60000)
        time_distance -= int_minute * 60000;
        int_second = Math.floor(time_distance / 1000)
        int_milliseconds = currentDate.getMilliseconds()


        // 时分秒为单数时、前面加零站位
        if (int_hour < 10)
          int_hour = "0" + int_hour;
        if (int_minute < 10)
          int_minute = "0" + int_minute;
        if (int_second < 10)
          int_second = "0" + int_second;
        if (int_milliseconds > 100)
          int_milliseconds = parseInt(int_milliseconds / 10);

        // 显示时间
        // time_d.innerHTML = int_day;

        timeData.hour = int_hour;
        timeData.minute = int_minute;
        timeData.second = int_second;
        timeData.milliseconds = int_milliseconds;
      } else {
        clearInterval(this.data.showTimeIntervalId)
        timeData.minute = '00';
        timeData.second = '00';
        timeData.milliseconds = '00';
      }
      this.setData({
        timeData: timeData
      })
    },
    getAuthPhoneNumber(e) {

     
      console.log('getAuthPhoneNumber', e)

      mobileApi.getAuthorizeMobile(e.detail.encryptedData, e.detail.sign, mobile => {
        console.log('mobile', mobile)

        let formData = this.data.formData
        formData.phone = mobile
        this.setData({
          formData: formData
        })

        this.submitMethod()
      }, err => {
        messageUtil.showFail('获取手机号失败')
        // todo 测试使用
        // let formData = this.data.formData
        // formData.phone = '13880104834'
        // this.setData({
        //   formData: formData
        // })
        // commonData.setA('123aaa')
        // commonData.setOpenid("123456")
        // console.log('set default phone', this.data.formData, this.data.formData.phone)
        // this.submitMethod()

      })


      // my.getPhoneNumber({
      //   success: (res) => {
      //     let response = res.response;
      //     response = JSON.parse(response)
      //     console.log('encryptedData', response)

      //     mobileApi.getAuthorizeMobile(response.response, response.sign, mobile => {
      //       console.log('mobile', mobile)

      //       let formData = this.data.formData
      //       formData.phone = mobile
      //       this.setData({
      //         formData: formData
      //       })

      //       this.submitMethod()
      //     }, err => {
      //       messageUtil.showFail('获取手机号失败')
      //       // todo 测试使用
      //       // let formData = this.data.formData
      //       // formData.phone = '13880104833'
      //       // this.setData({
      //       //   formData: formData
      //       // })
      //       // commonData.setA('666666')
      //       // commonData.setOpenid("123456")
      //       // console.log('set default phone', this.data.formData, this.data.formData.phone)
      //       // this.submitMethod()

      //     })

      //   },
      //   fail: (res) => {
      //     console.log('my.getPhoneNumber' + res);
      //   },
      // });


    },
    handleAuthError(e) {
      console.log('getPhoneNumber handleAuthError', e)
    },
    agreementConfirmOkHandler() {
      let formData = this.data.formData
      formData.isAgreement = true

      this.setData({
        formData: formData,
        isShowAgreementConfirm: false
      })
      this.submitMethod()
    },
    agreementConfirmCancelHandler() {
      this.setData({
        isShowAgreementConfirm: false
      })
    },
    agreementConfirmShowAgreementHandler(value) {
      let data = {
        target: {
          dataset: {
            attr: value
          }
        }
      }
      this.showAgreementHandler(data)
    },
    submitMethod() {

      // var reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      // if (!(reg_tel.test(this.data.formData.phone))) {
      //   messageUtil.showFail('手机号格式不正确');
      //   return;
      // }


      // formData.value.isAgreement = true;
      if (!this.data.formData.isAgreement) {
        // messageUtil.showFail('请先同意并阅读相关条款')
        console.log('submitMethod2')
        this.setData({
          isShowAgreementConfirm: true
        })
        return;
      }


      console.log('submit', this.data.formData)
      let params = {
        a: commonData.getA(),
        mobile: this.data.formData.phone,
        p_code:2024042604,
        type:2,
        productType:20,
        proType: 2,
        user_id: commonData.getOpenid(),
        proCode:'ZY1504',
        // returnUrl: 'http://keep.syhy000.com/keep/five/?mobile=' + this.data.formData.phone,
      }
      if(commonData.getParam()){
        params.param = commonData.getParam()
      }

      // let query = "mobile=" + this.data.formData.phone

      // params.returnUrl = 'http://keep.syhy000.com/h5GotoZfbMini/#/?appid=2021004135681441&url=pages/keeplq/keeplq&query=' + encodeURIComponent(query)
      messageUtil.showLoading()
      lbkOrderApi.submitOrders(params, res => {
        messageUtil.closeLoading()
       this.setData({
        oder:res.data.orderLocal
       })
       
        // my.ap.openURL({
        //   url: res.data.payUrl, // 请将 url 替换为有效的页面地址
        //   success: (res) => {
        //     console.log('openURL success', res)
        //   },
        //   fail: (err) => {
        //     console.log('openURL success', err)
        //   }
        // });

        my.paySignCenter({
          signStr: res.data.str,
          success: (paySignCenterRes) => {
            console.log('zhouqiPayHandler success ' + JSON.stringify(paySignCenterRes))
            console.log(paySignCenterRes.resultStatus);
            if (paySignCenterRes.resultStatus && parseInt(paySignCenterRes.resultStatus) == 6001) {
              messageUtil.closeLoading()
              messageUtil.showFail('支付失败')
              this.props.onGoToPay('支付失败')
              return;
            }else{
              this.props.onGoToPay('成功')
            }
           
          },
          fail: (paySignCenterResErr) => {
            console.log('zhouqiPayHandler fail ' + JSON.stringify(paySignCenterResErr))
            this.props.onGoToPay('支付失败')
            logApi.addKeepLog('龙宝卡拉起周期扣款失败：'  + JSON.stringify(paySignCenterResErr))
          }
        });

      }, err => {

      })
    },
    inputHandler(e) {
      this.setData({
        formData: {
          phone: e.detail.value
        },
      });
    },
    agreementChange(e) {
      this.setData({
        formData: {
          isAgreement: !this.data.formData.isAgreement
        }
      })
    },
    showAgreementHandler(e) {
      this.setData({
        agreementType: parseInt(e.target.dataset.attr),
        isShowAgreement: true
      })
    },
    agreementOkHandler() {
      this.setData({
        isShowAgreement: false
      })
    },
  },
});